<template>
	<view class="activist-container" :data-theme="themeStyle">
		<scroll-view scroll-x="true" class="order-nav">
			<view v-for="(statusItem, statusIndex) in statusList" :key="statusIndex" class="uni-tab-item" :id="statusItem.id" :data-current="statusIndex" @click="ontabtap">
				<text class="uni-tab-item-title" :class="statusItem.status == refundStatus ? 'uni-tab-item-title-active color-base-border  color-base-text' : ''">
					{{ statusItem.name }}
				</text>
			</view>
		</scroll-view>
		
		<mescroll-uni ref="mescroll" @getData="getListData" top="100rpx">
			<block slot="list">
				<view class="container">
					<block v-if="refundList.length">
						<view class="order-item" v-for="(item, index) in refundList" :key="index">
							<view class="order-header">
								<text class="status-num font-size-base">{{ item.order_no }}</text>
								<view class="status-name" v-if="item.refund_status == 3">退款成功</view>
								<view class="status-name color-base-text" v-if="item.refund_status == 1">退款中</view>
								<view class="status-name color-base-text" v-if="item.refund_status == -1">退款失败</view>
							</view>
							<view class="goods-wrap">
								<image :src="$util.img(item.sku_image, { size: 'mid' })" @error="imageError(index)" mode="aspectFill"
								 :lazy-load="true"></image>
								<view class="goods-info">
									<view class="goods-name" @click="refundDetail(item.order_goods_id)">{{ item.sku_name }}</view>
									<view class="goods-num">
										<view class="num-text color-base-text">{{ item.refund_status_name }}</view>
										<view class="num-price">
											<text>￥{{ item.price }}</text>
											<text class="num">×{{ item.num }}</text>
										</view>
									</view>
								</view>
							</view>
							<view class="goods-btn">
								<view class="btn-text">
									<text>共{{ item.num }}件商品</text>
									<text>退款：￥{{ item.refund_status == 3 ? item.refund_real_money : item.refund_apply_money }}</text>
								</view>
								<view class="order-action">
									<block v-if="cl_id == 0">
										<view class="order-box-btn" @click="refundDetail(item.order_goods_id)">{{ $lang('checkDetail') }}</view>
										<block v-if="item.refund_action.length">
											<view class="order-box-btn" @click="refundAction(actionItem.event, item)" v-for="(actionItem, actionIndex) in item.refund_action"
											 :key="actionIndex">
												{{ actionItem.title }}
											</view>
										</block>
									</block>
									<block v-else>
										<block v-if="item.leader_action.length">
											<view class="order-box-btn" @click="refundAction(actionItem.event, item)" v-for="(actionItem, actionIndex) in item.leader_action"
											 :key="actionIndex">
												{{ actionItem.title }}
											</view>
										</block>
									</block>
								</view>
							</view>
						</view>
					</block>
					<block v-else>
						<view class="cart-empty" v-if="showEmpty">
							<ns-empty :isIndex="!1" :text="$lang('emptyTips')"></ns-empty>
						</view>
					</block>
				</view>
			</block>
		</mescroll-uni>
		
		<loading-cover ref="loadingCover"></loading-cover>
	</view>
</template>

<script>
	import refundMethod from '../public/js/refundMethod.js';
	import globalConfig from '@/common/js/golbalConfig.js';

	export default {
		mixins: [refundMethod, globalConfig],
		data() {
			return {
				refundList: [],
				showEmpty: false,
				cl_id: 0,
				scrollInto: '',
				refundStatus: 'all',
				statusList: []
			};
		},
		components: {},
		onLoad(option) {
			if (option.cl_id) this.cl_id = option.cl_id;
			this.getRefundStatus();
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();

			if (!uni.getStorageSync('token')) {
				this.$util.redirectTo('/pages/order/login/login', {
					back: '/pages/order/activist/activist'
				});
			}
		},
		methods: {
			ontabtap(e) {
				let index = e.target.dataset.current || e.currentTarget.dataset.current;
				this.refundStatus = this.statusList[index].status;
				if (this.refundStatus == '') this.refundList = [];
				this.$refs.loadingCover.show();
				this.$refs.mescroll.refresh();
			},
			getRefundStatus() {
				this.$api.sendRequest({
					url: '/api/orderrefund/refundstatus',
					success: res => {
						if (res.code == 0) this.statusList = res.data;
					}
				})
			},
			getListData(mescroll) {
				let _cl_id = ''
				if (this.cl_id != 0) _cl_id = this.cl_id
				this.$api.sendRequest({
					url: '/api/orderrefund/lists',
					data: {
						page: mescroll.num,
						page_size: mescroll.size,
						cl_id: _cl_id,
						refund_status: this.refundStatus
					},
					success: res => {
						this.showEmpty = true;
						let newArr = [];
						let msg = res.message;
						if (res.code == 0 && res.data) {
							newArr = res.data.list;
						} else {
							this.$util.showToast({
								title: msg
							});
						}
						mescroll.endSuccess(newArr.length);
						//设置列表数据
						if (mescroll.num == 1) this.refundList = []; //如果是第一页需手动制空列表
						this.refundList = this.refundList.concat(newArr);
						this.$refs.loadingCover.hide();
					},
					fail: res => {
						mescroll.endErr();
						this.$refs.loadingCover.hide();
					}
				});
			},
			refundDetail(order_goods_id) {
				this.$util.redirectTo('/pages/order/refund_detail/refund_detail', {
					order_goods_id,
					cl_id:this.cl_id
				});
			},
			refundAction(event, data) {
				switch (event) {
					case 'orderRefundCancel': // 撤销维权
						this.cancleRefund(data.order_goods_id, res => {
							if (res.code >= 0) {
								this.$util.showToast({
									title: '撤销成功'
								});
								this.$refs.mescroll.refresh();
							}
						});
						break;
					case 'orderRefundDelivery': // 退款发货
						this.$util.redirectTo('/pages/order/refund_detail/refund_detail', {
							order_goods_id: data.order_goods_id,
							action: 'returngoods'
						});
						break;
					case 'orderRefundAsk':
						this.$util.redirectTo('/pages/order/refund/refund', {
							order_goods_id: data.order_goods_id
						});
						break;
					case 'leaderRefundTakeDelivery':
						uni.showModal({
							content: '您确定已收到用户退回的货物了吗？',
							success: res => {
								if (res.confirm) {
									this.$api.sendRequest({
										url: '/api/orderrefund/leaderconfirm',
										data: {
											order_goods_id: data.order_goods_id
										},
										success: res => {
											this.$refs.mescroll.refresh();
										}
									})
								}
							}
						})
						break;
				}
			},
			imageError(index) {
				this.refundList[index].sku_image = this.$util.getDefaultImage().default_goods_img;
				this.$forceUpdate();
			}
		}
	};
</script>

<style lang="scss" scoped>
	/deep/ .fixed {
		position: relative;
		top: 0;
	}

	.cart-empty {
		padding-top: 154px !important;
	}

	@import '../public/css/activist.scss';
</style>
